# Contributor: Chris Kruger <alpine@krugerheavyindustries.com>
# Contributor: Fabricio Silva <hi@fabricio.dev>
# Maintainer: Chris Kruger <alpine@krugerheavyindustries.com>
pkgname=vaultwarden
pkgver=1.30.1
pkgrel=0
pkgdesc="bitwarden alternative backend"
_bw_web_ver=2023.10.0
provides="bitwarden_rs=$pkgver-r$pkgrel"
replaces="bitwarden_rs"
url="https://github.com/dani-garcia/vaultwarden"
arch="all !ppc64le !s390x !riscv64" # ring crate has platform specific asm - no ppc support, no rust on s390x
license="AGPL-3.0-only"
makedepends="
	cargo
	cargo-auditable
	libpq-dev
	mariadb-dev
	mimalloc2-dev
	openssl-dev>3
	sqlite-dev
	"
subpackages="$pkgname-web-vault::noarch $pkgname-doc $pkgname-openrc"
install="$pkgname.pre-install"
pkgusers="vaultwarden"
pkggroups="vaultwarden"
source="$pkgname-$pkgver.tar.gz::https://github.com/dani-garcia/vaultwarden/archive/$pkgver.tar.gz
	$pkgname-webvault-$_bw_web_ver.tar.gz::https://github.com/dani-garcia/bw_web_builds/releases/download/v$_bw_web_ver/bw_web_v$_bw_web_ver.tar.gz
	$pkgname.initd
	$pkgname.confd"

prepare() {
	default_prepare

	mkdir -p .cargo

	local target="$(rustc -vV | sed -n 's/host: //p')"

	# Build against system-provided mimalloc.
	cat >> .cargo/config.toml <<-EOF
		[target.$target]
		mimalloc = { rustc-link-lib = ["mimalloc"] }
	EOF

	cargo fetch --target="$CTARGET" --locked
}

build() {
	export VW_VERSION=$pkgver-r$pkgrel
	cargo auditable build --release --features sqlite,postgresql,mysql,enable_mimalloc --frozen
}

check() {
	cargo test --release --features sqlite,postgresql,mysql,enable_mimalloc --frozen
}

package() {
	install -Dm755 target/release/vaultwarden -t "$pkgdir"/usr/bin/
	install -d -m750 -o vaultwarden -g vaultwarden "$pkgdir"/var/lib/vaultwarden "$pkgdir"/var/log/vaultwarden
	(cd "$srcdir"/web-vault; find . -type f -exec \
		install -Dm644 {} "$pkgdir"/usr/share/webapps/vaultwarden-web/{} \;)
	(cd "$builddir"/src/static; find . -type f -exec \
		install -Dm644 {} "$pkgdir"/usr/share/webapps/vaultwarden-web/{} \;)

	install -Dm644 README.md "$pkgdir"/usr/share/doc/vaultwarden/README.md
	install -Dm755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -Dm655 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname
}

vault() {
	pkgdesc="$pkgdesc (web ui)"

	amove usr/share/webapps

	# move templates back
	mkdir -p "$pkgdir"/usr/share/webapps/vaultwarden-web
	mv "$subpkgdir"/usr/share/webapps/vaultwarden-web/templates "$pkgdir"/usr/share/webapps/vaultwarden-web/
}

sha512sums="
4a03a7e132a0ed7fd5e0d994fa2544a649d7fafb136486658d7f801a7998fd7e9247c961811c20388f1bc4c98992184864ff03ac092cfa1c24c65e0c1139265b  vaultwarden-1.30.1.tar.gz
ef751df31af488cb288bfa5751626f0c5ecece110a7023a229ecd0a1b6678a9ee759ab4ed3acc1bb0be795de3429aa6229b515740ba7d359c46a984973bbb9ad  vaultwarden-webvault-2023.10.0.tar.gz
a407bf26f78b74f0d907c4cde1fca8ff00c3ba22093397ace903c8648c03bd943a064b1756337cf82e8aeb5e4617640fefd46565420e8812c6e0fc7392f6be5b  vaultwarden.initd
bd9aee73cf34bcbf5331d2edf78eb9d434f6371a4357c50d1316c58f2c29711c58956a1bc441fc0880b0ed60e7790540a8c0f90bc1701c5010b20e9e58050003  vaultwarden.confd
"
